package com.crud.project.controller;

import com.crud.project.po.Permission;
import com.crud.project.service.PermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class PermissionController {
    @Autowired
    private PermissionService permissionService;
    @GetMapping("/permission")
    public String permission(@RequestParam(required=true, defaultValue="1") Integer page, Model model){
        PageHelper.startPage(page,3);
        List<Permission> userList=permissionService.findAllPermission();
        PageInfo<Permission> pageInfo=new PageInfo<>(userList);
        model.addAttribute("permission",pageInfo);
        return "permission";
    }

    @GetMapping("/permission-input")
    public String permissionInput(Model model, HttpSession httpSession){
        if (httpSession.getAttribute("permission")==null){
            model.addAttribute("permission",new Permission());
        }
        return "permission-input";
    }

    @PostMapping("/insertPermission")
    public String insertPermission(Permission permission,RedirectAttributes redirectAttributes){
        List<Permission>permissionList=permissionService.findAllPermission();
        for (Permission permission1:permissionList){
            if (permission1.getPermission_name().equals(permission.getPermission_name())){
                redirectAttributes.addFlashAttribute("message","权限名已存在");
                return  "redirect:/permission";
            }
        }
        permissionService.insertPermission(permission);
        return  "redirect:/permission";

    }
    @GetMapping("/deletePermission/{id}")
    public String deletePermission(@PathVariable Long id){
            permissionService.deletePermissionById(id);
            return "redirect:/permission";
    }

    @GetMapping("/updatePermission-input/{id}")
    public String updateUserById(@PathVariable Long id,Model model){
        Permission permission=permissionService.findAllPermissionById(id);
        model.addAttribute("permission",permission);
        return "permission-input";
    }

    @PostMapping("/updatePermission/{id}")
    public String update(@PathVariable Long id,Permission permission){
        Permission permission1=permissionService.findAllPermissionById(id);
        permission.setPermission_id(permission1.getPermission_id());
        BeanUtils.copyProperties(permission, permission1);
        permissionService.updatePermission(permission1);
        return "redirect:/permission";
    }
}
